package cn.HuanG.org.controller;

import cn.HuanG.org.domain.Shop;
import cn.HuanG.org.query.ShopQuery;
import cn.HuanG.org.service.IShopService;
import cn.HuanG.util.AjaxResult;
import cn.HuanG.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * controller模块
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;
    @Autowired
    private JavaMailSender javaMailSender;

    @GetMapping
    public List<Shop> getAll(){
        return  shopService.queryAll();
    }

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Shop Shop){
        try {
            if(Shop.getId()==null){
                shopService.save(Shop);
            }else {
                shopService.update(Shop);
                Long id = Shop.getAdmin().getId();
                System.out.println(Shop.getAdmin());
                //创建复杂邮件
                MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                //复杂邮件构建的工具类
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                helper.setFrom("2062329819@qq.com");
                helper.setTo("15283180317@139.com");
                //设置主题
                helper.setSubject("交友+q");
                //以html格式解析内容
                helper.setText("<img src='https://47.95.117.210:8888/group1/M00/02/6F/rBE3kWIGRwCAAqAlAAIlCcZ96-4241.jpg'/>" +
                        "<a href='http://localhost:8081/#/shopActive?id="+id+"'>点击立即上门</a>",true);
                //添加附件
                // helper.addAttachment("相册选技师",new File("C:\\Users\\admin\\Desktop\\ss.rar"));

                javaMailSender.send(mimeMessage);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @GetMapping("/{id}") //查询，区分通过uri（id占位符）
    public Shop findById(@PathVariable("id") Long id){
       return shopService.queryById(id);
    }

    @PostMapping  //分页专用
    public PageInfo<Shop> page(@RequestBody ShopQuery query){
       return shopService.queryPage(query);

    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @PostMapping("/settlement")
    public AjaxResult settlement( @RequestBody Shop shop){
        System.out.println(shop);
        try {
            shopService.settlement(shop);
            return  AjaxResult.me();
        }catch (RuntimeException runtimeException) {
            runtimeException.printStackTrace();
            return AjaxResult.me().setMessage(runtimeException.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

}

























